<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 450px;
            padding: 40px;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
            font-weight: 600;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            transition: border-color 0.3s;
        }

        input:focus {
            border-color: #4a90e2;
            outline: none;
            box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.2);
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #4a90e2;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #3a7bc8;
        }

        .message {
            margin-top: 20px;
            padding: 12px;
            border-radius: 6px;
            text-align: center;
            display: none;
        }

        .error {
            background-color: #ffebee;
            color: #d32f2f;
            display: block;
        }

        .success {
            background-color: #e8f5e9;
            color: #388e3c;
            display: block;
        }

        .login-link {
            text-align: center;
            margin-top: 20px;
            color: #666;
        }

        .login-link a {
            color: #4a90e2;
            text-decoration: none;
        }

        .login-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>创建您的账户</h1>

    <form id="registerForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>

        <div class="form-group">
            <label for="email">电子邮箱</label>
            <input type="email" id="email" name="email" placeholder="请输入电子邮箱" required>
        </div>

        <div class="form-group">
            <label for="phone">手机号码</label>
            <input type="tel" id="phone" name="phone" placeholder="请输入手机号码" required>
        </div>

        <button type="submit">注册</button>
    </form>

    <div id="message" class="message"></div>

    <div class="login-link">
        已有账户？<a href="#">立即登录</a>
    </div>
</div>

<script>
    document.getElementById('registerForm').addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const email = document.getElementById('email').value;
        const phone = document.getElementById('phone').value;

        // 创建请求数据对象
        const userData = {
            username: username,
            password: password,
            email: email,
            phone: phone
        };

        // 显示加载状态
        const submitButton = this.querySelector('button[type="submit"]');
        submitButton.disabled = true;
        submitButton.textContent = '注册中...';

        // 发送POST请求
        fetch('/v1/user/reg', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(userData)
        })
            .then(response => response.json())
            .then(data => {
                const messageElement = document.getElementById('message');

                if (data.state === 2000) {
                    // 注册成功
                    messageElement.className = 'message success';
                    messageElement.textContent = data.message || '注册成功！';

                    // 清空表单
                    this.reset();

                    // 可以在这里添加跳转逻辑
                    // window.location.href = '/login.html';
                } else if (data.state === 1000) {
                    // 用户名被占用
                    messageElement.className = 'message error';
                    messageElement.textContent = data.message || '用户名已被占用，请尝试其他用户名';

                    // 聚焦到用户名输入框
                    document.getElementById('username').focus();
                } else {
                    // 其他错误
                    messageElement.className = 'message error';
                    messageElement.textContent = data.message || '注册失败，请稍后重试';
                }
            })
            .catch(error => {
                console.error('Error:', error);
                const messageElement = document.getElementById('message');
                messageElement.className = 'message error';
                messageElement.textContent = '网络错误，请检查您的连接';
            })
            .finally(() => {
                // 恢复按钮状态
                submitButton.disabled = false;
                submitButton.textContent = '注册';
            });
    });
</script>
</body>
</html>